package cc.customer.web.sys.controller.system;

import cc.customer.common.domain.ResponseBo;
import cc.customer.common.domain.Tree;
import cc.customer.common.utils.FileUtils;
import cc.customer.system.domain.Collage;
import cc.customer.system.service.CollageService;
import cc.customer.web.sys.controller.base.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CollageController extends BaseController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CollageService collageService;


    @RequestMapping(value = "collage/addDialog")
    public  String addDialog(){return "system/sale/collage_add";}

    @RequestMapping(value = "collage/updateDialog")
    public  String editDialog(){return "system/sale/collage_update";}


    @RequestMapping(value = "collage/getAllCollage")
    @ResponseBody
    public ResponseBo getAllCollage(){
        try {
            Tree<Collage> collage = collageService.getAllCollage();
            return ResponseBo.ok(collage);
        }catch (Exception e){
            log.error("获取高校失败:"+e.getMessage());
            e.printStackTrace();
        }
        return ResponseBo.error("获取失败");
    }
    /**查询所有的高校**/
    @RequestMapping(value = "collage/getCollageList")
    @ResponseBody
    public List<Collage> getCollageList(Collage collagesll){
        System.out.println(System.currentTimeMillis());
        try {
            List<Collage>collage = collageService.getAllCollageByExample(collagesll);
            System.out.println(System.currentTimeMillis());
            return collage;
        }catch (Exception e){
            log.error("获取高校失败:"+e.getMessage());
            e.printStackTrace();
        }

        return new ArrayList<>();
    }
    /**根据parentid 获取高校的学院或者专业**/
    @RequestMapping(value = "collage/getCollageByParentId")
    @ResponseBody
    public List<Collage> getCollageByParentId(Collage  collage){
        try {
            List<Collage>collageparen = collageService.getCollageByParentId(collage);
            return collageparen;
        }catch (Exception e){
            log.error("获取高校失败:"+e.getMessage());
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**根据名字查询高校的专业**/
    @RequestMapping(value = "collage/getCollagePref")
    @ResponseBody
    public List<Collage> getCollagePref(Collage collagesll){
        try {
            List<Collage>collage = collageService.getAllCollageByExample(collagesll);
            if(collage.size()<=0){
                return new ArrayList<>();
            }
            List<Collage>collageOref = collageService.getCollagePref(collage.get(0).getCollageId());
            return collageOref;
        }catch (Exception e){
            log.error("获取高校失败:"+e.getMessage());
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 导出
     * **/
    @RequestMapping(value = "collage/excel")
    @ResponseBody
    public ResponseBo exportCollage(Collage collage){
        try {
            List<Collage>collage1 = collageService.getCollageByExample(collage);
            return FileUtils.createExcelByPOIKit("高校列表",collage1,Collage.class);
        }catch (Exception e){
            log.error("获取部门信息excel失败", e);
            return ResponseBo.error("导出excel失败，请联系网站管理员！");
        }
    }
    /**
     * 获取详情
     * **/
    @RequestMapping(value = "collage/getCollageInfo")
    @ResponseBody
    public ResponseBo getCollageInfo(Long collageId ){
        if (collageId !=null){
            Collage collage  = collageService.getCollageByKey(collageId);
            return ResponseBo.ok(collage);
        }
        return ResponseBo.error("获取高校详情失败");
    }

    /**
     * 删除
     * **/
    @RequestMapping(value = "collage/delete")
    @ResponseBody
    public ResponseBo deleteCollage(String ids){
        if(StringUtils.isEmpty(ids)){
            return ResponseBo.error("删除失败");
        }
        try {
            collageService.deleteCollage(ids);
        }catch (Exception E){
            return ResponseBo.error("删除失败 :"+E.getMessage());
        }
        return ResponseBo.ok("删除成功");
    }

    /**增加高校信息**/
    @RequestMapping(value = "collage/addCollage")
    @ResponseBody
    public ResponseBo addCollage(Collage collage){
        try {
            collageService.addCollage(collage);
        }catch (Exception e){
            return ResponseBo.error("新增失败");
        }
        return ResponseBo.ok("新增成功");
    }

   /**
    * 修改高校或者专业信息
    * **/
   @RequestMapping(value = "collage/updateCollage")
    @ResponseBody
    public ResponseBo updateCollage(Collage collage){
       if(StringUtils.isEmpty(collage.getCollageId().toString())){
           return ResponseBo.error("获取参数为空");
       }
       try {
           collageService.updateCollage(collage);
            return ResponseBo.ok("修改成功...");
       }catch (Exception e){
           log.error("更新高校信息失败："+e.getMessage());
       }
       return ResponseBo.error("更新失败");
   }

//    @RequestMapping(value = "collage/daoru")
//    public String daoru(Long l){
//        File files = new File("C:/Users/2018051601/Desktop/12345.xlsx");
//        try {
//            List<Collage> list = new ArrayList<>();
//            Workbook rwb= new XSSFWorkbook(new FileInputStream(files));
//            Sheet sheet = rwb.getSheetAt(0);
//            int rows = sheet.getLastRowNum();//获取所有得行
//            for(int i=0;i<=rows;i++){
//                Collage col = new Collage();
//                Row row = sheet.getRow(i);
//                col.setParentId(l);
//                System.out.println(rows);
//                System.out.println(row.getCell(0).getNumericCellValue()+"------------");
//                System.out.println(row.getCell(1).getStringCellValue()+"------------");
//                String num = String.valueOf(row.getCell(0).getNumericCellValue()).substring(0,6);
//                col.setCollageCode(Long.valueOf(num));
//                col.setCollageName(row.getCell(1).getStringCellValue());
//                list.add(col);
//            }
//            collageService.batchInsert(list);
//        }catch (Exception e ){
//            e.printStackTrace();
//        }
//        return "yes";
//    }
}
